
package com.hlkj.pay.config;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import cn.hutool.json.JSONConfig;
import com.hlkj.framework.common.util.json.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ExtRocketMQTemplateConfiguration;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert;

/**
 * @author monk@techsun.com
 * @date 2024/09/11 17:08
 */
@ExtRocketMQTemplateConfiguration(nameServer = "${rocketmq.name-server}")
@Slf4j
public class ExtRocketMQTemplate extends RocketMQTemplate {

    /**
     * 发送RocketMQ
     * 
     * @param topic
     * @param object
     */
    public void send(String topic, Object object) {
        JSONConfig jsonConfig = new JSONConfig();
        jsonConfig.setIgnoreNullValue(true);
        String msg = JsonUtils.toJsonStr(object, jsonConfig);
        Assert.hasText(topic, "topic cannot be null!");
        log.debug("发送RocketMQ topic:{} object:{}", topic, object);
        Message<String> message = MessageBuilder.withPayload(msg).build();
        send(topic, message);
    }

    /**
     * 发送RocketMQ
     * 
     * @param topic
     * @param object
     */
    public void sendDelay(Integer delay, String topic, Object object) {
        // 延迟消息支持的时间 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
        JSONConfig jsonConfig = new JSONConfig();
        jsonConfig.setIgnoreNullValue(true);
        String msg = JsonUtils.toJsonStr(object, jsonConfig);
        Assert.hasText(topic, "topic cannot be null!");
        log.debug("发送 RocketMQ 延迟 topic:{} object:{}", topic, object);
        Message<String> message = MessageBuilder.withPayload(msg).build();
        syncSend(topic, message, 10000, delay);
    }
}
